package com.eduadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.eduadmin.entity.Page;
import com.eduadmin.entity.TeachInfo;
import com.eduadmin.entity.Teacher;
import com.eduadmin.listener.TeachInfoDataUploadListener;
import com.eduadmin.service.ITeachInfoService;
import com.eduadmin.service.ITeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TeachInfoController {
    //构造函数自动装配
    private final ITeachInfoService teachInfoService;
    private final ITeacherService teacherService;

    @Autowired
    public TeachInfoController(ITeacherService teacherService, ITeachInfoService teachInfoService) {
        this.teachInfoService = teachInfoService;
        this.teacherService = teacherService;
    }

    // 获取所有教师任教信息
    @RequestMapping(value = "getAllTeachInfo")
    @ResponseBody
    public Map<String, Object> getAllTeachInfo(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            @RequestParam(value = "teacherName", required = false, defaultValue = "") String teacherName   //页面搜索框参数
    ) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());
        paraMap.put("teacherName", teacherName);

        List<TeachInfo> teachInfoListTemp = teachInfoService.getAllTeachInfo(paraMap);
        List<TeachInfo> teachInfoList = new ArrayList<>();
        //把英文课程换成中文名字
        for (TeachInfo teachInfo : teachInfoListTemp) {
            String courseEnName = teachInfo.getCourseEnName();
            switch (courseEnName) {
                case "chinese":
                    teachInfo.setCourseEnName("语文");
                    break;
                case "maths":
                    teachInfo.setCourseEnName("数学");
                    break;
                case "english":
                    teachInfo.setCourseEnName("英语");
                    break;
                case "physics":
                    teachInfo.setCourseEnName("物理");
                    break;
                case "history":
                    teachInfo.setCourseEnName("历史");
                    break;
                case "chemistry":
                    teachInfo.setCourseEnName("化学");
                    break;
                case "biology":
                    teachInfo.setCourseEnName("生物");
                    break;
                case "politics":
                    teachInfo.setCourseEnName("政治");
                    break;
                case "geography":
                    teachInfo.setCourseEnName("地理");
                    break;
                case "sports":
                    teachInfo.setCourseEnName("体育");
                    break;
            }
            teachInfoList.add(teachInfo);
        }
        Long total = teachInfoService.getAllTotal(paraMap);
        reMap.put("rows", teachInfoList);
        reMap.put("total", total);

        return reMap;
    }


    //导入任教信息
    @RequestMapping(value = "addTeachInfos")
    @ResponseBody
    public String addTeachInfos(MultipartFile teachInfoFile) throws IOException {
        EasyExcel.read(teachInfoFile.getInputStream(), TeachInfo.class, new TeachInfoDataUploadListener(teacherService, teachInfoService)).sheet().doRead();
        return "上传成功";
    }

    // 新增 任教信息
    @ResponseBody
    @RequestMapping(value = "newAddTeachInfo")
    public Map<String, String> newAddTeachInfo(TeachInfo teachInfo) {
        Map<String, String> ret = new HashMap<>();
        List<TeachInfo> teachInfoList = new ArrayList<>();
        //判断该教师是否在教师表中
        Teacher teacher = teacherService.getTeacherByTeacherIdcard(teachInfo.getTeacherIdCard());
        if (teacher == null) {
            ret.put("type", "error");
            ret.put("msg", "增加失败，该教师身份证号未在系统中 ！");
            return ret;
        }
        String teamName = teachInfo.getTeamName();
        teachInfo.setTeamName(teachInfo.getGradeName() + teamName);

        //判断是否存在 该任教信息
        List<TeachInfo> ths = teachInfoService.getTeachInfo(teachInfo);
        if (ths.size() == 0) {
            teachInfoList.add(teachInfo);
            teachInfoService.addTeachInfos(teachInfoList);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        } else {
            ret.put("type", "error");
            ret.put("msg", "增加失败，教师任教信息已存在！");
        }
        return ret;
    }

    // 修改任教信息
    @ResponseBody
    @RequestMapping(value = "editTeachInfo")
    public Map<String, String> editTeachInfo(TeachInfo teachInfo) {
        Map<String, String> ret = new HashMap<>();
        //判断该教师是否在教师表中
        Teacher teacher = teacherService.getTeacherByTeacherIdcard(teachInfo.getTeacherIdCard());
        if (teacher == null) {
            ret.put("type", "error");
            ret.put("msg", "增加失败，该教师身份证号未在系统中 ！");
            return ret;
        }
        //课程中文名换成英文名
        if(this.checkgbk(teachInfo.getCourseEnName())){
            switch (teachInfo.getCourseEnName()){
                case "语文":
                    teachInfo.setCourseEnName("chinese");
                    break;
                case "数学":
                    teachInfo.setCourseEnName("maths");
                    break;
                case "英语":
                    teachInfo.setCourseEnName("english");
                    break;
                case "物理":
                    teachInfo.setCourseEnName("physics");
                    break;
                case "化学":
                    teachInfo.setCourseEnName("chemistry");
                    break;
                case "生物":
                    teachInfo.setCourseEnName("biology");
                    break;
                case "地理":
                    teachInfo.setCourseEnName("geography");
                    break;
                case "政治":
                    teachInfo.setCourseEnName("politics");
                    break;
                case "历史":
                    teachInfo.setCourseEnName("history");
                    break;
                case "体育":
                    teachInfo.setCourseEnName("sports");
                    break;
            }
        }
        if (teachInfo.getTeamName().length() == 3) {
            teachInfo.setTeamName(teachInfo.getGradeName() + teachInfo.getTeamName());    //年级+班级号=完整的班级名称
        }
        List<TeachInfo> ths = teachInfoService.getTeachInfo(teachInfo);
        if (ths.size() > 0) {
            if(ths.get(0).getTeachId() == teachInfo.getTeachId()){
                teachInfoService.editTeachInfo(teachInfo);
                ret.put("type", "success");
                ret.put("msg", "修改成功！");
            }else {
                ret.put("type", "error");
                ret.put("msg", "更新失败 ，该任课信息已经存在！");
            }
        } else {
            ths.clear();
            ths.add(teachInfo);
            teachInfoService.editTeachInfo(teachInfo);
            ret.put("type", "success");
            ret.put("msg", "更新成功！");
        }
        return ret;
    }

    //删除 任教信息
    @RequestMapping(value = "deleteTeachInfo")
    @ResponseBody
    public Map<String, String> deleteTeachInfo(@RequestParam(value = "ids[]") long[] teachIdArray) {
        Map<String, String> ret = new HashMap<>();
        int n = teachInfoService.deleteTeachInfo(StringUtils.join(teachIdArray, ','));
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除成功！");
            return ret;
        }
        ret.put("type", "error");
        ret.put("msg", "删除失败！");
        return ret;
    }

    //判断字符串是否是汉字
    public boolean checkgbk(String name) {
        int n = 0;
        for (int i = 0; i < name.length(); i++) {
            n = (int) name.charAt(i);
            if (!(19968 <= n && n < 40869)) {
                return false;
            }
        }
        return true;
    }

}
